package com.excel.controller;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.excel.entity.JsonExcel;
import com.excel.service.ExcelImportService;
import com.excel.service.UploadDownloadService;
import com.utils.JsonResult;

/**
 * @Title: ExcelImportController.java
 * @Package com.excel.controller
 * @author 孙梦楠
 * @date 2017年11月3日 上午9:16:26
 * @version V1.0.0
 */
@RestController
@RequestMapping("/import")
public class ExcelImportController {

	@Autowired
	private ExcelImportService excelimportservice;

	@Autowired
	private UploadDownloadService uploaddownloadservice;

	@RequestMapping(value = "", method = RequestMethod.POST)
	public JsonResult<String> excelImport(

			@RequestParam(value = "file", required = true) MultipartFile file,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie)
			throws FileNotFoundException, IOException {

//		MultipartFile file = jsonexcel.getFile();
		


			String filename = uploaddownloadservice.upload(file);
//			String filename = jsonexcel.getFilename();
			return excelimportservice.excelImport(filename, opeorgcookie, opetypecookie, languagecookie);
		
		
			
	}

	@RequestMapping(value = "/", method = RequestMethod.POST)
	public String Import(@RequestParam MultipartFile file,HttpServletRequest request) {
//		if (request instanceof MultipartHttpServletRequest) {
//		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//		MultipartFile file = multipartRequest.getFile("file");
			return "input";
//		} else
//			return "error";
	}
	
	

}
